<?php
namespace Common\Model;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/27
 * Time: 19:29
 */
class GoodsModel extends BaseModel
{
	// 主键名称
	protected $pk               =   'gid';
	// 数据表名（不包含表前缀）
	protected $tableName        =   'goods';
	// 自动验证定义
	protected $_validate        =  [
		[ 'gname' , 'require' , '请输入商品名称' ],
		[ 'gnumber' , 'require' , '请输入商品数量' ],
		[ 'shopprice' , 'require' , '请输入商品价格' ],

	];
	public function store($id)
	{
		echo $id;
		//1.商品表验证
		if ( !$this->create() ) {
			return [ 'status' => 'faild' , 'message' => $this->getError() ];
			exit;
		}
		//商品详情表验证
		$detailModel = new DetailModel();
		if (!$detailModel->create()){
			return [ 'status' => 'faild' , 'message' => $detailModel->getError() ];
			exit;
		}
		if ( !isset( $_POST[ 'detailPic' ] ) ) {
			return [ 'status' => 'faild' , 'message' => '请上传商品图册' ];
			exit;
		}

        //执行添加
		//1.商品表添加
		$data = [
			'gname'       => $_POST[ 'gname' ] ,
			'gnumber'     => $_POST[ 'gnumber' ] ,
			'unit'        => $_POST[ 'unit' ] ,
			'marketprice' => $_POST[ 'marketprice' ] ,
			'shopprice'   => $_POST[ 'shopprice' ] ,
			'pic'         => $_POST[ 'pic' ] ,
			'click'       => $_POST[ 'click' ] ,
			'time'        => time() ,
			'aid'         => 1 ,//aid后台管理员id，我这里没有登录，写死1，从session里面取出来即可
			'bid'         => $_POST[ 'bid' ] ,
			'cid'         => $_POST[ 'cid' ] ,
			'tid'         => $_POST[ 'tid' ] ,
			'total'       => $_POST[ 'total' ] ,
		];
		if ($id){
			$gid = $id;
			 $this->where("gid={$gid}")->save( $data );

		}else{
			$gid = $this->add( $data );
		}
		//2.执行商品详情表添加
		//处理大中小图
		$image  = new \Think\Image();
		$small  = '';
		$medium = '';
		$big    = '';
		foreach ( $_POST[ 'detailPic' ] as $k => $v ) {
			$image->open( $v );
			// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
			//处理路径
			$smallPath  = dirname( $v ) . '/small_' . basename( $v );
			$mediumPath = dirname( $v ) . '/medium_' . basename( $v );
			$bigPath    = dirname( $v ) . '/big_' . basename( $v );
			//缩略
			$image->thumb( 800 , 800 )->save( $bigPath );
			$image->thumb( 400 , 400 )->save( $mediumPath );
			$image->thumb( 80 , 80 )->save( $smallPath );
			//得到路径
			$small .= $smallPath . ',';
			$medium .= $mediumPath . ',';
			$big .= $bigPath . ',';
		}
		$data = [
			'small'   => rtrim($small,',') ,
			'medium'  => rtrim($medium,',')  ,
			'big'     => rtrim($big,',')  ,
			'intro'   => $_POST[ 'intro' ] ,
			'service' => $_POST[ 'service' ] ,
			'gid'     => $gid ,
		];
		if ($id){
			echo 2;
			$detailModel->where("gid={$gid}")->save( $data );
		}else{
			$detailModel->add( $data );
		}
//		echo 3;
		//3.商品属性表添加
		$goodsAttrModel = new GoodsAttrModel();
		//属性添加
		foreach($_POST['attr'] as $k=>$v){
			if($v){
				$data = ['gtvalue'=>$v,'added'=>0,'taid'=>$k,'gid'=>$gid];
				if ($id){
					echo 3;
					$goodsAttrModel->where("gid={$gid}")->save($data);
				}else{
					$goodsAttrModel->add($data);
				}
			}
		}
		//规格添加
		foreach($_POST['spec'] as $k=>$v){
			foreach($v['tavalue'] as $kk=>$vv){
				$data = ['gtvalue'=>$vv,'added'=>$v['added'][$kk],'taid'=>$k,'gid'=>$gid];
				if ($id){
					echo 4;
					$goodsAttrModel->where("gid={$gid}")->save($data);
				}else{
					$goodsAttrModel->add($data);
				}
			}
		}
		return ['status'=>'success','message'=>'操作成功'];


	}
	public function del($gid){
		$this->delete($gid);//商品表goods删除

		(new GoodsAttrModel())->where("gid={$gid}")->delete();//商品属性表goodsattr删除

		(new DetailModel())->where(['gid'=>$gid])->delete();//商品详情表detail删除

		return ['status'=>'success','message'=>'操作成功'];
	}

}